package com.gxuwz.controller;

import com.gxuwz.entity.Classes;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.Code;
import com.gxuwz.result.PageInfo;
import com.gxuwz.result.ResultInfo;
import com.gxuwz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description:班级管理模块
 */

@Controller
@RequestMapping("/classes")
public class ClassesController {
    @Autowired
    private ClassesService classesService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentService studentService;
    

    @Autowired
    private InstructorService instructorService;

    /**
     * @Description:查询班级信息，并跳转至班级信息管理页面
     * @Param:  [com.gxuwz.result.PageInfo, org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Classes]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/list")
    public ModelAndView classesList(PageInfo pageInfo, ModelAndView modelAndView, Classes classes,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        PageVo pageVo = classesService.getClassesList(classes,pageInfo,username);
        List<String> grades = classesService.getClassGrades(username);
        List<String> majors = classesService.getMajors(username);

        modelAndView.addObject("grades",grades);
        modelAndView.addObject("majors",majors);

        modelAndView.setViewName("classesManage/classesManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:跳转至添加班级信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, javax.servlet.http.HttpServletRequest]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/addClassesPage")
    public ModelAndView queryIds(ModelAndView modelAndView, HttpServletRequest request){
        List<String> ids = classesService.selectClassesIds();
        HttpSession session = request.getSession();
        String  instID = ((String) session.getAttribute("username"));
        String depID = instructorService.getDepIDByInstID(instID);
        modelAndView.addObject("depID",depID);
        modelAndView.addObject("classesIds",ids);
        modelAndView.setViewName("classesManage/addClasses");
        return modelAndView;
    }

    /**
     * @Description:实现添加班级信息功能,并跳转至班级信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Classes]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/addClasses")
    public ModelAndView addClasses(ModelAndView modelAndView,Classes classes,HttpServletRequest request){
        HttpSession session = request.getSession();
        String  username = (String) session.getAttribute("username");
        boolean flag = classesService.addClassesInfo(classes);
        System.out.println(classes.toString());
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_OK,"新增成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_ERR,"新增失败"));
        }
        PageVo pageVo = classesService.getClassesList(null,pageInfo,username);
        List<String> grades = classesService.getClassGrades(username);
        List<String> majors = classesService.getMajors(username);
        modelAndView.addObject("grades",grades);
        modelAndView.addObject("majors",majors);
        modelAndView.setViewName("classesManage/classesManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现班级信息单独删除功能，并跳转至班级信息管理页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteClassesOne")
    public ModelAndView deleteClassesOne(String classId, ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flagStudent = studentService.selectStudentExist(classId);
        boolean flagCourse = courseService.selectCourseExist(classId);
        PageInfo pageInfo = new PageInfo();
        if(flagCourse&&flagStudent){
            boolean flag = classesService.deleteClassesOne(classId);
            if(flag){
                modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"删除成功"));
            } else {
                modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败"));
            }
            PageVo pageVo = classesService.getClassesList(null,pageInfo,username);
            List<String> grades = classesService.getClassGrades(username);
            List<String> majors = classesService.getMajors(username);
            modelAndView.addObject("grades",grades);
            modelAndView.addObject("majors",majors);
            modelAndView.setViewName("classesManage/classesManage");
            modelAndView.addObject("pageVo",pageVo);
            return modelAndView;
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败,请先处理该班级下的课程和学生！！！"));
            PageVo pageVo = classesService.getClassesList(null,pageInfo,username);
            List<String> grades = classesService.getClassGrades(username);
            List<String> majors = classesService.getMajors(username);
            modelAndView.addObject("grades",grades);
            modelAndView.addObject("majors",majors);
            modelAndView.setViewName("classesManage/classesManage");
            modelAndView.addObject("pageVo",pageVo);
            return modelAndView;
        }

    }

    /**
     * @Description:跳转至编辑班级信息管理页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/updateClassesPage")
    public ModelAndView queryClassesOne(String classId,ModelAndView modelAndView){
        System.out.println(classId);
        Classes classesOne = classesService.getClassesOne(classId);
        System.out.println(classesOne);
        List<String> ids = classesService.selectClassesIds();
        modelAndView.addObject("classesIds",ids);
        modelAndView.addObject("classesOne",classesOne);
        modelAndView.setViewName("classesManage/editClasses");
        return modelAndView;
    }

    /**
     * @Description:实现修改班级信息功能，并跳转至班级信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Classes, java.lang.String]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/updateClasses")
    public ModelAndView updateClasses(ModelAndView modelAndView,Classes classes,String oldId,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flag = classesService.updateClassesInfo(classes,oldId);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改失败"));
        }
        PageVo pageVo = classesService.getClassesList(null, pageInfo,username);
        List<String> grades = classesService.getClassGrades(username);
        List<String> majors = classesService.getMajors(username);
        modelAndView.addObject("grades",grades);
        modelAndView.addObject("majors",majors);
        modelAndView.setViewName("classesManage/classesManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

}
